package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil; 
import java.math.BigDecimal;
import java.util.Calendar;

@Repository("AdditiveaddressDAO")
@Transactional
public class AdditiveaddressDAOImpl extends AbstractJpaDao<Additiveaddress> implements AdditiveaddressDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Additiveaddress.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public AdditiveaddressDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Additiveaddress findAdditiveaddressById(Integer id) throws DataAccessException {

		return findAdditiveaddressById(id, -1, -1);
	}

	@Transactional
	public Additiveaddress findAdditiveaddressById(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findAdditiveaddressById", startResult, maxRows, id);
			return (Additiveaddress) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Additiveaddress> findAdditiveaddressByName(String name) throws DataAccessException {

		return findAdditiveaddressByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Additiveaddress> findAdditiveaddressByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findAdditiveaddressByName", startResult, maxRows, name);
		return new LinkedHashSet<Additiveaddress>(query.getResultList());
	}

	@Transactional
	public Set<Additiveaddress> findAdditiveaddressByNameContaining(String name) throws DataAccessException {

		return findAdditiveaddressByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Additiveaddress> findAdditiveaddressByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findAdditiveaddressByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Additiveaddress>(query.getResultList());
	}

	@Transactional
	public Set<Additiveaddress> findAdditiveaddressByTime(Calendar time) throws DataAccessException {

		return findAdditiveaddressByTime(time, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Additiveaddress> findAdditiveaddressByTime(Calendar time, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findAdditiveaddressByTime", startResult, maxRows, time);
		return new LinkedHashSet<Additiveaddress>(query.getResultList());
	}

	@Transactional
	public Set<Additiveaddress> findAdditiveaddressByPrice(BigDecimal price) throws DataAccessException {

		return findAdditiveaddressByPrice(price, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Additiveaddress> findAdditiveaddressByPrice(BigDecimal price, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findAdditiveaddressByPrice", startResult, maxRows, price);
		return new LinkedHashSet<Additiveaddress>(query.getResultList());
	}

	@Transactional
	public Set<Additiveaddress> findAdditiveaddressByDescription(String description) throws DataAccessException {

		return findAdditiveaddressByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Additiveaddress> findAdditiveaddressByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findAdditiveaddressByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Additiveaddress>(query.getResultList());
	}

	@Transactional
	public Set<Additiveaddress> findAdditiveaddressByDescriptionContaining(String description)
			throws DataAccessException {

		return findAdditiveaddressByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Additiveaddress> findAdditiveaddressByDescriptionContaining(String description, int startResult,
			int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAdditiveaddressByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Additiveaddress>(query.getResultList());
	}

	@Transactional
	public Additiveaddress findAdditiveaddressByPrimaryKey(Integer id) throws DataAccessException {
		return findAdditiveaddressByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Additiveaddress findAdditiveaddressByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findAdditiveaddressByPrimaryKey", startResult, maxRows, id);
			return (Additiveaddress) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Additiveaddress> findAdditiveaddresssByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findAdditiveaddresssByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Additiveaddress> findAllAdditiveaddresss() throws DataAccessException {

		return findAllAdditiveaddresss(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Additiveaddress> findAllAdditiveaddresss(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllAdditiveaddresss", startResult, maxRows);
		return new LinkedHashSet<Additiveaddress>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Additiveaddress> findAdditiveaddresssByAdditive(Integer additiveId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllAdditiveaddresssByAdditive", start, max, additiveId);
		return new LinkedHashSet<Additiveaddress>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Additiveaddress> findAdditiveaddresssByAddress(Integer addressId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllAdditiveaddresssByAddress", start, max, addressId);
		return new LinkedHashSet<Additiveaddress>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Additiveaddress> findAdditiveaddresssByUnit(Integer unitId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllAdditiveaddresssByUnit", start, max, unitId);
		return new LinkedHashSet<Additiveaddress>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Additiveaddress> findAdditiveaddressByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Additiveaddress>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Additiveaddress> findAdditiveaddresssByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Additiveaddress.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Additiveaddress> additiveaddresss = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return additiveaddresss;
	}

	@Transactional
	public Additiveaddress findAdditiveaddressByPhysicalPrimaryKey(Integer additiveId, Integer addressId, Calendar time)
			throws DataAccessException {
		return findAdditiveaddressByPhysicalPrimaryKey(additiveId, addressId, time, -1, -1);
	}

	@Transactional
	public Additiveaddress findAdditiveaddressByPhysicalPrimaryKey(Integer additiveId, Integer addressId, Calendar time,
			int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findAdditiveaddressByPhyscialPrimaryKey", startResult, maxRows, additiveId,
					addressId, time);
			return (Additiveaddress) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Additiveaddress entity) {
		return true;
	}
}
